package repository

import (
	"context"
	"database/sql"
	"fmt"
	"strconv"
	"strings"

	"mall/common/enumx"
	"mall/common/modelx"
	"mall/service/blog/model"

	"github.com/volatiletech/sqlboiler/v4/boil"
	"github.com/volatiletech/sqlboiler/v4/queries"
	"github.com/volatiletech/sqlboiler/v4/queries/qm"
	"github.com/zeromicro/go-zero/core/stores/cache"
)

type (
	// BlogPostRepository is an interface to be customized, add more methods here,
	// and implement the added methods in customBlogPostRepository.
	BlogPostRepository interface {
		Conn() error
		FindOne(ctx context.Context, id int64, cols []string) (*model.BlogPost, error) ///
		GetList(
			ctx context.Context,
			cols model.M,
			withCount int,
			filter map[string]map[string][]any,
		) ([]*model.BlogPost, int64, error) /////
		Insert(
			ctx context.Context,
			post *model.BlogPost,
			detail *model.BlogPostDetail,
			extra *model.BlogExtra,
		) error /////
		Update(
			ctx context.Context,
			post *model.BlogPost,
			detail *model.BlogPostDetail,
			extra *model.BlogExtra,
		) (int64, error) /////
		UpdateBatch(ctx context.Context, ids []int64, cols model.M) (int64, error) //////
		RestoreBatch(ctx context.Context, ids []int64, cols model.M) (int64, error)
		ForceDeleteBatch(ctx context.Context, ids []int64) (int64, error) //////
		GetDetail(
			ctx context.Context,
			cols model.M,
			withCount int,
			filter map[string]map[string][]any,
		) ([]*model.BlogPostDetail, int64, error)
		GetMeta(
			ctx context.Context,
			cols model.M,
			withCount int,
			filter map[string]map[string][]any,
		) ([]*model.BlogPostMetum, int64, error) /////
		GetMedia(
			ctx context.Context,
			cols model.M,
			withCount int,
			filter map[string]map[string][]any,
		) ([]*model.BlogAttachmentRelationship, int64, error) //////

	}

	customBlogPostRepository struct {
		cacheConf  cache.CacheConf
		dataSource string

		mysqlConn *sql.DB
	}
)

// NewBlogPostRepository returns a repository for the database table.
func NewBlogPostRepository(dataSource string, cacheConf cache.CacheConf) BlogPostRepository {
	return &customBlogPostRepository{
		cacheConf:  cacheConf,
		dataSource: dataSource,
	}
}

func (r *customBlogPostRepository) Conn() error {
	if r.mysqlConn == nil {
		mysqlConn, err := modelx.NewMysqlConn(r.dataSource)
		if err != nil {
			return err
		}

		r.mysqlConn = mysqlConn
	}

	return nil
}

func (r *customBlogPostRepository) FindOne(
	ctx context.Context,
	id int64,
	cols []string,
) (post *model.BlogPost, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	post, err = model.FindBlogPost(ctx, r.mysqlConn, uint64(id), cols...)
	if err == sql.ErrNoRows {
		return nil, model.ErrNotFound
	}

	return
}

func (r *customBlogPostRepository) GetList(
	ctx context.Context,
	cols model.M,
	withCount int,
	filter map[string]map[string][]any,
) (list []*model.BlogPost, count int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	var mods []qm.QueryMod
	var page int
	var pageSize int
	var offset int
	var limit int
	var selectCol string
	for k, v := range cols {
		switch k {
		case "page":
			if val, ok := v.(int64); ok && val > 1 {
				page = int(val)
			}
		case "pageSize":
			if val, ok := v.(int64); ok && val > 0 {
				pageSize = int(val)
			}
		case "offset":
			if val, ok := v.(int32); ok && val > 0 {
				offset = int(val)
			}
		case "limit":
			if val, ok := v.(int32); ok && val > 0 {
				limit = int(val)
			}
		case "orderBy":
			if val, ok := v.(string); ok && val != "" {
				mods = append(mods, qm.OrderBy(val))
			}
		case "select":
			if val, ok := v.(string); ok && val != "" {
				selectCol = val
			}
		case "asPreview":
			if val, ok := v.(string); ok && val == "" { // 不是预览，必须是已发布
				mods = append(mods, model.BlogPostWhere.PostStatus.EQ(uint16(enumx.PostStatusPublish)))
			}
		case "search":
			if val, ok := v.(string); ok && val != "" { // 搜索
				likeVal := fmt.Sprintf("%%%s%%", val)
				mods = append(mods, qm.Where(fmt.Sprintf(
					"(%s LIKE ? OR %s LIKE ?)",
					model.BlogPostColumns.PostTitle,
					model.BlogPostColumns.PostName), likeVal, likeVal))
			}
		}
	}

	if filter != nil {
		for key, opMap := range filter {
			switch key {
			case model.BlogPostColumns.ID:
				mods = append(mods, modelx.GetIntMods(model.BlogPostColumns.ID, opMap)...)
			case model.BlogPostColumns.PostAuthorID:
				mods = append(mods, modelx.GetIntMods(model.BlogPostColumns.PostAuthorID, opMap)...)
			case model.BlogPostColumns.PostParentID:
				mods = append(mods, modelx.GetIntMods(model.BlogPostColumns.PostParentID, opMap)...)
			case model.BlogPostColumns.PostTitle:
				mods = append(mods, modelx.GetStringMods(model.BlogPostColumns.PostTitle, opMap)...)
			case model.BlogPostColumns.PostExcerpt:
				mods = append(mods, modelx.GetStringMods(model.BlogPostColumns.PostExcerpt, opMap)...)
			case model.BlogPostColumns.PostStatus:
				mods = append(mods, modelx.GetIntMods(model.BlogPostColumns.PostStatus, opMap)...)
			case model.BlogPostColumns.CommentStatus:
				mods = append(mods, modelx.GetIntMods(model.BlogPostColumns.CommentStatus, opMap)...)
			case model.BlogPostColumns.PostName:
				mods = append(mods, modelx.GetStringMods(model.BlogPostColumns.PostName, opMap)...)
			}
		}
	}

	if withCount >= 0 {
		if withCount >= 2 {
			var nextCount int64
			var previousCount int64
			subMods := make([]qm.QueryMod, len(mods), len(mods)+3)
			copy(subMods, mods)
			subMods = append(subMods, qm.Limit(1))
			subMods = append(subMods, qm.Select(model.BlogPostColumns.ID))

			if withCount != 3 && offset >= 1 {
				subMods = append(subMods, qm.Offset(offset-1))
				previousList, previousErr := model.BlogPosts(subMods...).All(ctx, r.mysqlConn)
				if previousErr != nil {
					err = previousErr
					return
				}
				if len(previousList) > 0 {
					previousCount = 1
				}
			}
			if limit > 0 {
				subMods = append(subMods, qm.Offset(offset+limit))
				nextList, nextErr := model.BlogPosts(subMods...).All(ctx, r.mysqlConn)
				if nextErr != nil {
					err = nextErr
					return
				}
				if len(nextList) > 0 {
					nextCount = 1
				}
			}

			count = previousCount + nextCount*2
		} else {
			count, err = model.BlogPosts(mods...).Count(ctx, r.mysqlConn)
			if err != nil {
				return
			}

			if withCount == 0 {
				return
			}
		}
	}

	if pageSize > 0 {
		if page > 1 {
			mods = append(mods, qm.Offset(pageSize*(page-1)))
		}
		mods = append(mods, qm.Limit(pageSize))
	} else if limit > 0 || offset > 0 {
		if limit > 0 {
			mods = append(mods, qm.Limit(limit))
		}
		if offset > 0 {
			mods = append(mods, qm.Offset(offset))
		}
	}

	var columns []string
	if selectCol != "" {
		if selectCol != "," {
			columns = strings.Split(selectCol, ",")
		}
		columns = append(columns, model.BlogPostColumns.ID)
	}
	if len(columns) > 0 {
		mods = append(mods, qm.Select(columns...))
	}

	list, err = model.BlogPosts(mods...).All(ctx, r.mysqlConn)
	return
}

func (r *customBlogPostRepository) Insert(
	ctx context.Context,
	post *model.BlogPost,
	detail *model.BlogPostDetail,
	extra *model.BlogExtra,
) error {
	if err := r.Conn(); err != nil {
		return err
	}

	tx, err := r.mysqlConn.BeginTx(ctx, nil)
	if err != nil {
		return err
	}

	err = post.Insert(ctx, tx, boil.Infer())
	if err != nil {
		tx.Rollback()
		return err
	}

	pid := post.ID
	detail.PostID = pid
	err = detail.Insert(ctx, tx, boil.Infer())
	if err != nil {
		tx.Rollback()
		return err
	}

	if len(extra.Meta) > 0 {
		for _, m := range extra.Meta {
			m.PostID = pid
			err = m.Insert(ctx, tx, boil.Infer())
			if err != nil {
				tx.Rollback()
				return err
			}
		}
	}

	objectType := uint16(enumx.PostTypePost)
	if len(extra.CategoryIds) > 0 {
		for _, cid := range extra.CategoryIds {
			var relationShip model.BlogTermRelationship
			relationShip.ObjectID = pid
			relationShip.ObjectType = objectType
			relationShip.TermID = uint64(cid)
			err = relationShip.Insert(ctx, tx, boil.Infer())
			if err != nil {
				tx.Rollback()
				return err
			}
		}
	}
	if len(extra.TagIds) > 0 {
		for _, tid := range extra.TagIds {
			var relationShip model.BlogTermRelationship
			relationShip.ObjectID = pid
			relationShip.ObjectType = objectType
			relationShip.TermID = uint64(tid)
			err = relationShip.Insert(ctx, tx, boil.Infer())
			if err != nil {
				tx.Rollback()
				return err
			}
		}
	}
	if len(extra.Media) > 0 {
		for _, m := range extra.Media {
			m.ObjectID = pid
			m.ObjectType = objectType
			err = m.Insert(ctx, tx, boil.Infer())
			if err != nil {
				tx.Rollback()
				return err
			}
		}
	}

	tx.Commit()
	return nil
}

func (r *customBlogPostRepository) Update(
	ctx context.Context,
	post *model.BlogPost,
	detail *model.BlogPostDetail,
	extra *model.BlogExtra,
) (int64, error) {
	if err := r.Conn(); err != nil {
		return 0, err
	}

	tx, err := r.mysqlConn.BeginTx(ctx, nil)
	if err != nil {
		return 0, err
	}

	postId, err := post.Update(ctx, tx, boil.Blacklist(
		model.BlogPostColumns.PostAuthorID,
		model.BlogPostColumns.PostParentID,
		model.BlogPostColumns.CommentCount,
	))
	if err != nil {
		tx.Rollback()
		return 0, err
	}

	pid := post.ID
	detail.PostID = pid
	_, err = detail.Update(ctx, tx, boil.Blacklist(
		model.BlogPostDetailColumns.DetailID,
		model.BlogPostDetailColumns.PostID,
		model.BlogPostDetailColumns.PingStatus,
		model.BlogPostDetailColumns.ToPing,
		model.BlogPostDetailColumns.Pinged,
	))
	if err != nil {
		tx.Rollback()
		return 0, err
	}

	if len(extra.Meta) > 0 {
		for _, m := range extra.Meta {
			if m.MetaID > 0 {
				_, err = m.Update(ctx, tx, boil.Blacklist(
					model.BlogPostMetumColumns.MetaID,
					model.BlogPostMetumColumns.PostID,
				))
			} else {
				m.PostID = pid
				err = m.Insert(ctx, tx, boil.Infer())
			}

			if err != nil {
				tx.Rollback()
				return 0, err
			}
		}
	}

	objectType := uint16(enumx.PostTypePost)
	if len(extra.Terms) > 0 {
		for _, t := range extra.Terms {
			if t.TermRelationshipID > 0 {
				_, err = t.Update(ctx, tx, boil.Blacklist(
					model.BlogTermRelationshipColumns.TermRelationshipID,
					model.BlogTermRelationshipColumns.ObjectID,
					model.BlogTermRelationshipColumns.ObjectType,
					model.BlogTermRelationshipColumns.TermID,
				))
				if err != nil {
					tx.Rollback()
					return 0, err
				}
			}
		}
	}
	if len(extra.CategoryIds) > 0 {
		for _, cid := range extra.CategoryIds {
			var relationShip model.BlogTermRelationship
			relationShip.ObjectID = pid
			relationShip.ObjectType = objectType
			relationShip.TermID = uint64(cid)
			err = relationShip.Insert(ctx, tx, boil.Infer())
			if err != nil {
				tx.Rollback()
				return 0, err
			}
		}
	}
	if len(extra.TagIds) > 0 {
		for _, tid := range extra.TagIds {
			var relationShip model.BlogTermRelationship
			relationShip.ObjectID = pid
			relationShip.ObjectType = objectType
			relationShip.TermID = uint64(tid)
			err = relationShip.Insert(ctx, tx, boil.Infer())
			if err != nil {
				tx.Rollback()
				return 0, err
			}
		}
	}
	if len(extra.Media) > 0 {
		for _, m := range extra.Media {
			if m.AttachmentRelationshipID > 0 {
				_, err = m.Update(ctx, tx, boil.Blacklist(
					model.BlogAttachmentRelationshipColumns.AttachmentRelationshipID,
					model.BlogAttachmentRelationshipColumns.ObjectID,
					model.BlogAttachmentRelationshipColumns.ObjectType,
					model.BlogAttachmentRelationshipColumns.AttachmentID,
				))
			} else {
				m.ObjectID = pid
				m.ObjectType = objectType
				err = m.Insert(ctx, tx, boil.Infer())
			}
			if err != nil {
				tx.Rollback()
				return 0, err
			}
		}
	}
	if len(extra.DelMetaIds) > 0 {
		ids := make([]any, 0, len(extra.DelMetaIds))
		for _, id := range extra.DelMetaIds {
			ids = append(ids, id)
		}
		var mods []qm.QueryMod
		mods = append(mods, qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogPostMetumColumns.MetaID), ids...))
		_, err := model.BlogPostMeta(mods...).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}
	}
	if len(extra.DelTermIds) > 0 {
		ids := make([]any, 0, len(extra.DelTermIds))
		for _, id := range extra.DelTermIds {
			ids = append(ids, id)
		}
		var mods []qm.QueryMod
		mods = append(mods, qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogTermRelationshipColumns.TermRelationshipID), ids...))
		_, err := model.BlogTermRelationships(mods...).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}
	}
	if len(extra.DelMediaIds) > 0 {
		ids := make([]any, 0, len(extra.DelMediaIds))
		for _, id := range extra.DelMediaIds {
			ids = append(ids, id)
		}
		var mods []qm.QueryMod
		mods = append(mods, qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogAttachmentRelationshipColumns.AttachmentRelationshipID), ids...))
		_, err := model.BlogAttachmentRelationships(mods...).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}
	}

	tx.Commit()
	return postId, nil
}

func (r *customBlogPostRepository) UpdateBatch(ctx context.Context, ids []int64, cols model.M) (rowsAff int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	var mods []qm.QueryMod
	if len(ids) > 0 {
		values := make([]any, 0, len(ids))
		for _, id := range ids {
			values = append(values, uint64(id))
		}

		mods = append(mods, qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogPostColumns.ID), values...))
	}

	rowsAff, err = model.BlogPosts(mods...).UpdateAll(ctx, r.mysqlConn, cols)
	return
}

func (r *customBlogPostRepository) RestoreBatch(ctx context.Context, ids []int64, cols model.M) (rowsAff int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	if len(ids) > 0 {
		values := make([]string, 0, len(ids))
		for _, id := range ids {
			values = append(values, strconv.FormatInt(id, 10))
		}

		var info RandomInfo
		err = queries.Raw(fmt.Sprintf(
			`update %s set %s=%s where %s in (%s)`,
			model.TableNames.BlogPosts,
			model.BlogPostColumns.PostStatus,
			model.BlogPostColumns.DelStatus,
			model.BlogPostColumns.ID,
			strings.Join(values, ","),
		)).Bind(ctx, r.mysqlConn, &info)
	}
	return
}

func (r *customBlogPostRepository) ForceDeleteBatch(ctx context.Context, ids []int64) (rowsAff int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	tx, err := r.mysqlConn.BeginTx(ctx, nil)
	if err != nil {
		return 0, err
	}

	if len(ids) > 0 {
		var mods []qm.QueryMod
		values := make([]any, 0, len(ids))
		for _, id := range ids {
			values = append(values, uint64(id))
		}
		mods = append(mods, qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogPostColumns.ID), values...))
		rowsAff, err = model.BlogPosts(mods...).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}

		mod := qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogPostDetailColumns.PostID), values...)
		_, err = model.BlogPostDetails(mod).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}

		mod = qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogPostMetumColumns.PostID), values...)
		_, err = model.BlogPostMeta(mod).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}

		var termRelationshipMods []qm.QueryMod
		termRelationshipMods = append(
			termRelationshipMods,
			qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogTermRelationshipColumns.ObjectID), values...),
		)
		termRelationshipMods = append(
			termRelationshipMods,
			model.BlogTermRelationshipWhere.ObjectType.EQ(uint16(enumx.PostTypePost)),
		)
		_, err = model.BlogTermRelationships(termRelationshipMods...).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}

		var attachmentRelationshipMods []qm.QueryMod
		attachmentRelationshipMods = append(
			attachmentRelationshipMods,
			qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogAttachmentRelationshipColumns.ObjectID), values...),
		)
		attachmentRelationshipMods = append(
			attachmentRelationshipMods,
			model.BlogAttachmentRelationshipWhere.ObjectType.EQ(uint16(enumx.PostTypePost)),
		)
		_, err = model.BlogAttachmentRelationships(attachmentRelationshipMods...).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}
	}

	tx.Commit()
	return
}

func (r *customBlogPostRepository) GetDetail(
	ctx context.Context,
	cols model.M,
	withCount int,
	filter map[string]map[string][]any,
) (list []*model.BlogPostDetail, count int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	var mods []qm.QueryMod
	var page int
	var pageSize int
	var selectCol string
	for k, v := range cols {
		switch k {
		case "page":
			if val, ok := v.(int64); ok && val > 1 {
				page = int(val)
			}
		case "pageSize":
			if val, ok := v.(int64); ok && val > 0 {
				pageSize = int(val)
			}
		case "orderBy":
			if val, ok := v.(string); ok && val != "" {
				mods = append(mods, qm.OrderBy(val))
			}
		case "select":
			if val, ok := v.(string); ok && val != "" {
				selectCol = val
			}
		}
	}

	if filter != nil {
		for key, opMap := range filter {
			switch key {
			case model.BlogPostDetailColumns.DetailID:
				mods = append(mods, modelx.GetIntMods(model.BlogPostDetailColumns.DetailID, opMap)...)
			case model.BlogPostDetailColumns.PostID:
				mods = append(mods, modelx.GetIntMods(model.BlogPostDetailColumns.PostID, opMap)...)
			}
		}
	}

	if withCount >= 0 {
		count, err = model.BlogPostDetails(mods...).Count(ctx, r.mysqlConn)
		if err != nil {
			return
		}

		if withCount == 0 {
			return
		}
	}

	if pageSize > 0 {
		if page > 1 {
			mods = append(mods, qm.Offset(pageSize*(page-1)))
		}
		mods = append(mods, qm.Limit(pageSize))
	}

	var columns []string
	if selectCol != "" {
		if selectCol != "," {
			columns = strings.Split(selectCol, ",")
		}
		columns = append(columns, model.BlogPostDetailColumns.DetailID)
	}
	if len(columns) > 0 {
		mods = append(mods, qm.Select(columns...))
	}

	list, err = model.BlogPostDetails(mods...).All(ctx, r.mysqlConn)
	return
}

func (r *customBlogPostRepository) GetMeta(
	ctx context.Context,
	cols model.M,
	withCount int,
	filter map[string]map[string][]any,
) (list []*model.BlogPostMetum, count int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	var mods []qm.QueryMod
	var page int
	var pageSize int
	var selectCol string
	for k, v := range cols {
		switch k {
		case "page":
			if val, ok := v.(int64); ok && val > 1 {
				page = int(val)
			}
		case "pageSize":
			if val, ok := v.(int64); ok && val > 0 {
				pageSize = int(val)
			}
		case "orderBy":
			if val, ok := v.(string); ok && val != "" {
				mods = append(mods, qm.OrderBy(val))
			}
		case "select":
			if val, ok := v.(string); ok && val != "" {
				selectCol = val
			}
		}
	}

	if filter != nil {
		for key, opMap := range filter {
			switch key {
			case model.BlogPostMetumColumns.MetaID:
				mods = append(mods, modelx.GetIntMods(model.BlogPostMetumColumns.MetaID, opMap)...)
			case model.BlogPostMetumColumns.PostID:
				mods = append(mods, modelx.GetIntMods(model.BlogPostMetumColumns.PostID, opMap)...)
			case model.BlogPostMetumColumns.MetaKey:
				mods = append(mods, modelx.GetStringMods(model.BlogPostMetumColumns.MetaKey, opMap)...)
			}
		}
	}

	if withCount >= 0 {
		count, err = model.BlogPostMeta(mods...).Count(ctx, r.mysqlConn)
		if err != nil {
			return
		}

		if withCount == 0 {
			return
		}
	}

	if pageSize > 0 {
		if page > 1 {
			mods = append(mods, qm.Offset(pageSize*(page-1)))
		}
		mods = append(mods, qm.Limit(pageSize))
	}

	var columns []string
	if selectCol != "" {
		if selectCol != "," {
			columns = strings.Split(selectCol, ",")
		}
		columns = append(columns, model.BlogPostMetumColumns.MetaID)
	}
	if len(columns) > 0 {
		mods = append(mods, qm.Select(columns...))
	}

	list, err = model.BlogPostMeta(mods...).All(ctx, r.mysqlConn)
	return
}

func (r *customBlogPostRepository) GetMedia(
	ctx context.Context,
	cols model.M,
	withCount int,
	filter map[string]map[string][]any,
) (list []*model.BlogAttachmentRelationship, count int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	var mods []qm.QueryMod
	var page int
	var pageSize int
	var selectCol string
	for k, v := range cols {
		switch k {
		case "page":
			if val, ok := v.(int64); ok && val > 1 {
				page = int(val)
			}
		case "pageSize":
			if val, ok := v.(int64); ok && val > 0 {
				pageSize = int(val)
			}
		case "orderBy":
			if val, ok := v.(string); ok && val != "" {
				mods = append(mods, qm.OrderBy(val))
			}
		case "select":
			if val, ok := v.(string); ok && val != "" {
				selectCol = val
			}
		}
	}

	if filter != nil {
		for key, opMap := range filter {
			switch key {
			case model.BlogAttachmentRelationshipColumns.AttachmentRelationshipID:
				mods = append(mods, modelx.GetIntMods(model.BlogAttachmentRelationshipColumns.AttachmentRelationshipID, opMap)...)
			case model.BlogAttachmentRelationshipColumns.ObjectID:
				mods = append(mods, modelx.GetIntMods(model.BlogAttachmentRelationshipColumns.ObjectID, opMap)...)
			case model.BlogAttachmentRelationshipColumns.ObjectType:
				mods = append(mods, modelx.GetIntMods(model.BlogAttachmentRelationshipColumns.ObjectType, opMap)...)
			case model.BlogAttachmentRelationshipColumns.AttachmentID:
				mods = append(mods, modelx.GetIntMods(model.BlogAttachmentRelationshipColumns.AttachmentID, opMap)...)
			case model.BlogAttachmentRelationshipColumns.AttachmentThumbnail:
				mods = append(mods, modelx.GetIntMods(model.BlogAttachmentRelationshipColumns.AttachmentThumbnail, opMap)...)
			case model.BlogAttachmentRelationshipColumns.AttachmentOrder:
				mods = append(mods, modelx.GetIntMods(model.BlogAttachmentRelationshipColumns.AttachmentOrder, opMap)...)
			}
		}
	}

	if withCount >= 0 {
		count, err = model.BlogAttachmentRelationships(mods...).Count(ctx, r.mysqlConn)
		if err != nil {
			return
		}

		if withCount == 0 {
			return
		}
	}

	if pageSize > 0 {
		if page > 1 {
			mods = append(mods, qm.Offset(pageSize*(page-1)))
		}
		mods = append(mods, qm.Limit(pageSize))
	}

	var columns []string
	if selectCol != "" {
		if selectCol != "," {
			columns = strings.Split(selectCol, ",")
		}
		columns = append(columns, model.BlogAttachmentRelationshipColumns.AttachmentRelationshipID)
		columns = append(columns, model.BlogAttachmentRelationshipColumns.ObjectID)
	}
	if len(columns) > 0 {
		mods = append(mods, qm.Select(columns...))
	}

	list, err = model.BlogAttachmentRelationships(mods...).All(ctx, r.mysqlConn)
	return
}
